package com.service.jkt.impl;

import com.dao.*;
import com.entity.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.jkt.YeWuServise;
import com.util.gzp.AjaxResponse;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@org.springframework.stereotype.Service
public class YeWuServiseImpl implements YeWuServise {
    @Resource
    private ServiceDao serviceDao;
    @Resource
    private ServicetypeDao servicetypeDao;
    @Resource
    private ServicestatusDao servicestatusDao;
    @Resource
    private ClientDao clientDao;
    @Resource
    private DissentingregistrationDao dissentingregistrationDao;
    @Resource
    private OwnershipDao ownershipDao;
    @Resource
    private ExistmortgageDao existmortgageDao;
    @Resource
    private BeinmortgageDao beinmortgageDao;
    @Resource
    private CloseregistrationDao closeregistrationDao;
    @Resource
    private RoomDao roomDao;
    @Resource
    private OtherregistrationDao otherregistrationDao;
    @Resource
    private CheckerDao checkerDao;

    @Override
    @Transactional
    public AjaxResponse ShowYeWu(Service service,Integer PageNum,Integer PageSize) {

        //查询主业务表
        List<Service> serviceList= serviceDao.findByServicestatusid(service.getServicestatusid());
        //资料录入时查询
        if(service.getServicestatusid()==1) {//声明用来装预告的数组
            List<Service> serv = new ArrayList<>();
            //查询所有
            List<Service> serviceAll = serviceDao.findAllUnit(null);
            for (int i = 0; i < serviceAll.size(); i++) {
                if (serviceAll.get(i).getServicetypeid() == 2 && serviceAll.get(i).getServicestatusid() == 4) {
                    serv.add(serviceAll.get(i));
                }
            }
            //预告装入
            if(serv!=null){
                serviceList.addAll(serv);
            }

        }
        //查询业务类型表
        List<Servicetype> servicetypeList= servicetypeDao.findAll();
        //查询业务状态表
        List<Servicestatus> servicestatusList=servicestatusDao.findAll();
        //查询客户表(包含代理人,申请人)
        List<Client> clientList = clientDao.selectByExample(null);

        //声明用来装业务类型名称的数组
        ArrayList servicetypes =new ArrayList();
        //声明用来装业务状态名称的数组
        ArrayList servicestatus =new ArrayList();
        //声明用来装申请人(proposer)的数组
        ArrayList proposers =new ArrayList();
        //声明用来装代理人(agent)的数组
        ArrayList agents = new ArrayList();
        //声明用来装受理时间的数组
        ArrayList stime = new ArrayList();
        //声明用来装终审人id的数组
        ArrayList uid = new ArrayList();
        //声明用来装房屋id的数组
        ArrayList roomids = new ArrayList();
        //声明用来装楼栋表id的数组
        //比对,然后给数组赋值
        for (int i = 0; i < serviceList.size(); i++) {
            System.out.println("对比循环-------------次数"+i);

            proposers.add(clientDao.selectByPrimaryKey( serviceList.get(i).getProposer()).getClientname());
           if(serviceList.get(i).getAgent()!=null) {
                agents.add(clientDao.selectByPrimaryKey(serviceList.get(i).getAgent()).getClientname());
            }
           Checker checker = checkerDao.findByServiceid(serviceList.get(i).getServiceid());
           if(checker!=null&&checker.toString().length()!=0&&checker.getFinalreviewerid()!=null){
               uid.add(checker.getFinalreviewerid());
           }

            for (int j = 0; j < servicetypeList.size(); j++) {
              //  System.out.println("对比业务类型"+serviceList.get(i).getServicetypeid()+" 和 "+servicetypeList.get(j).getServicetypeid());
                if(serviceList.get(i).getServicetypeid()==servicetypeList.get(j).getServicetypeid()){
                    servicetypes.add(servicetypeList.get(j).getServicetype());
                 //   System.out.println("装入"+servicetypeList.get(j).getServicetype());
                    break;
                }
            }
            for (int j = 0; j < servicestatusList.size(); j++) {
                if(serviceList.get(i).getServicestatusid()==servicestatusList.get(j).getServicestatusid()){
                    servicestatus.add(servicestatusList.get(j).getServicestatus());
                    break;
                }
            }

//            for (int j = 0; j < clientList.size(); j++) {
//                if(serviceList.get(i).getProposer()==clientList.get(j).getClientid()){
//                    proposers.add(clientList.get(i).getClientname());
//                }
//            }
//            if(serviceList.get(i).getAgent()!=null){
//                for (int j = 0; j < clientList.size(); j++) {
//                    agents.add(clientList.get(i).getClientname());
//                    break;
//                }
//                }else{
//                agents.add("无");
//            }

            switch ( serviceList.get(i).getServicetypeid() ){
                case 1 :  //所有权
                    Ownership os = ownershipDao.findByServiceNumber(serviceList.get(i).getServicenumber());
                    stime.add(os.getRegistertime());
                    if(os.getRoomid()!=null){
                        roomids.add(os.getRoomid());
                    }else{roomids.add(0);}
                    break;
                case 2: //预售(预告)
                case 5: //预告登记
                    Otherregistration ot = otherregistrationDao.findByServiceid(serviceList.get(i).getServiceid());
                    stime.add(ot.getRegistertime());
                    if(serviceList.get(i).getPhoto()!=null){
                            roomids.add(serviceList.get(i).getPhoto());
                    }else{roomids.add(0);}
                    break;
                case 3: //他权项现房
                    Existmortgage ex = existmortgageDao.findByServiceid(serviceList.get(i).getServiceid());
                    stime.add(ex.getRegistertime());
                    if(ex.getRoomid()!=null){
                        roomids.add(ex.getRoomid());
                    }else{roomids.add(0);}
                    break;
                case 4: //他权项在建
                    Beinmortgage be = beinmortgageDao.findByServiceid(serviceList.get(i).getServiceid());
                    stime.add(be.getRegistertime());
                    if(be.getRoomid()!=null){
                        roomids.add(be.getRoomid());
                    }else{roomids.add(0);}
                    break;
                case 6://查封登记
                    Closeregistration cl = closeregistrationDao.findByServiceid(serviceList.get(i).getServiceid());
                    stime.add(cl.getRegistertime());
                    if(cl.getDescript()!=null){
                        roomids.add(cl.getDescript());
                    }else{roomids.add(0);}
                    break;
                case 7://异议登记
                    Dissentingregistration di=dissentingregistrationDao.findByServiceid(serviceList.get(i).getServiceid());
                    stime.add(di.getRegistertime());
                    if(di.getDescript()!=null){
                        roomids.add(di.getDescript());
                    }else{roomids.add(0);}
                    break;
                case 8://查封注销
                    Closeregistration clo = closeregistrationDao.findByServiceid(serviceList.get(i).getServiceid());
                    stime.add(clo.getOpenregistertime());
                    if(clo.getDescript()!=null){
                        roomids.add(clo.getDescript());
                    }else{roomids.add(0);}
                    break;
            }
        }
        //分页
        PageHelper.startPage(PageNum,PageSize);
        PageInfo<Service> pageInfo =new PageInfo<>(serviceList);
        Map<String,Object> map =new HashMap<>();
        map.put("service",pageInfo);
        map.put("servicetype",servicetypes);
        map.put("servicestatus",servicestatus);
        map.put("proposers",proposers);
        map.put("agents",agents);
        map.put("time",stime);
        map.put("uid",uid);
        map.put("roomids",roomids);
    return AjaxResponse.success(map,"success");
    }
    //所有权
    @Override
    public AjaxResponse syqLuRu(Ownership ownership, Service service) {
//          roomDao.insertSelective(room);
//          Room rm =  roomDao.findByRoomnumber(room.getRoomnumber());
//          Integer rmid= rm.getRoomid();
//          ownership.setRoomid(rmid);
//        Ownership own =ownershipDao.findByServiceNumber(service.getServicenumber());
//        Integer ow =  own.getOsid();
//        ownership.setOsid(ow);
        try{
            service.setServicestatusid(2);
            ownershipDao.updateByPrimaryKey(ownership);
            serviceDao.updateByPrimaryKeySelective(service);
        }catch (Exception e){
            System.out.println("------------------所有权录入失败！");
        }
        return AjaxResponse.success(true,"success");

    }

    //他权项现房
    @Override
    @Transactional
    public AjaxResponse txqxfLuRu(Existmortgage existmortgage, Service service) {
        try{
            service.setServicestatusid(2);
            existmortgageDao.updateByPrimaryKey(existmortgage);
            serviceDao.updateByPrimaryKeySelective(service);
        }catch (Exception e){
            System.out.println("------------------他权项（现房）录入失败！");
        }
        return AjaxResponse.success(true,"success");

    }
    //他权项在建
    @Override
    @Transactional
    public AjaxResponse txzjfLuRu(Beinmortgage beinmortgage, Service service) {
        try{
            service.setServicestatusid(2);
            beinmortgageDao.updateByPrimaryKey(beinmortgage);
            serviceDao.updateByPrimaryKeySelective(service);
        }catch (Exception e){
            System.out.println("------------------他权项（在建）录入失败！");
        }
        return AjaxResponse.success(true,"success");

    }

    //预告
    @Override
    public AjaxResponse ygLuRu(Service service,String i1, String i2) {
        try{
       serviceDao.updateByPrimaryKeySelective(service);
       Otherregistration oth = otherregistrationDao.findByServiceid(service.getServiceid());
       oth.setRoomid(Integer.valueOf(service.getPhoto()));
       oth.setObligee(service.getProposer());
       oth.setBoligeenumber(i1);
       oth.setObligornumber(i2);
       oth.setObligor(service.getAgent());
       oth.setRegistrationtypeid(2);
       otherregistrationDao.updateByPrimaryKeySelective(oth);
    }catch (Exception e){
        System.out.println("------------------预告登记录入失败！");
    }
        return AjaxResponse.success(true,"success");
    }

    //异议
    @Override
    @Transactional
    public AjaxResponse yyLuRu(Dissentingregistration dissentingregistration, Service service) {
        try{
            service.setServicestatusid(2);
            dissentingregistrationDao.updateByPrimaryKey(dissentingregistration);
            serviceDao.updateByPrimaryKeySelective(service);
        }catch (Exception e){
            System.out.println("------------------异议登记录入失败！");
        }
        return AjaxResponse.success(true,"success");
    }

    //查封
    @Override
    @Transactional
    public AjaxResponse cfLuru(Closeregistration closeregistration, Service service) {
        try{
            service.setServicestatusid(2);
            closeregistrationDao.updateByPrimaryKey(closeregistration);
            serviceDao.updateByPrimaryKeySelective(service);
        }catch (Exception e){
            System.out.println("-----------------查封登记录入失败！");
        }
        return AjaxResponse.success(true,"success");
    }
    //解封
    @Override
    public AjaxResponse jfLuru(Closeregistration closeregistration, Service service) {
        try{
            service.setServicestatusid(2);
            closeregistrationDao.updateByPrimaryKey(closeregistration);
            serviceDao.updateByPrimaryKeySelective(service);
        }catch (Exception e){
            System.out.println("-----------------解封登记录入失败！");
        }
        return AjaxResponse.success(true,"success");
    }
}
